package org.atovio.coolexam.controller;

import org.atovio.coolexam.common.Result;
import org.atovio.coolexam.entity.ExamPaper;
import org.atovio.coolexam.entity.ExamPaperQuestion;
import org.atovio.coolexam.service.ExamPaperQuestionService;
import org.atovio.coolexam.service.ExamPaperService;
import org.atovio.coolexam.service.SubjectService;
import org.atovio.coolexam.viewmodel.admin.exampaper.PaperAddSubmitVM;
import org.atovio.coolexam.viewmodel.admin.exampaper.TableRowVM;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/examPapers")
public class ExamPaperController {

    private final ExamPaperQuestionService examPaperQuestionService;
    private final ExamPaperService examPaperService;
    private final SubjectService subjectService;

    @Autowired
    public ExamPaperController(ExamPaperQuestionService examPaperQuestionService, ExamPaperService examPaperService, SubjectService subjectService) {
        this.examPaperQuestionService = examPaperQuestionService;
        this.examPaperService = examPaperService;
        this.subjectService = subjectService;
    }

    @PostMapping
    public Result add(@RequestBody PaperAddSubmitVM paperVM) {
        ExamPaper examPaper = new ExamPaper();
        examPaper.setName(paperVM.getName());
        examPaper.setSubjectId(paperVM.getSubjectId());
        examPaper.setSuggestTime(paperVM.getSuggestTime());
        examPaper.setQuestionCount(paperVM.getQuestionCount());
        examPaper.setScore(paperVM.getScore());
        ExamPaper dbEP = examPaperService.add(examPaper);

        Integer examPaperId = dbEP.getId();
        List<Integer> questionIds = paperVM.getQuestionIds();
        questionIds.forEach(q -> {
            ExamPaperQuestion examPaperQuestion = new ExamPaperQuestion();
            examPaperQuestion.setExamPaperId(examPaperId);
            examPaperQuestion.setQuestionId(q);

            examPaperQuestionService.add(examPaperQuestion);
        });

        return Result.success();
    }

    @GetMapping("/criteria")
    public Result query(@RequestParam Integer id, @RequestParam String name) {
        List<TableRowVM> tableData = new ArrayList<>();
        List<ExamPaper> examPapers = examPaperService.getSelective(
                Map.of("id", id, "name", name)
        );
        examPapers.forEach(ep -> {
            TableRowVM tableRowVM = new TableRowVM();
            tableRowVM.setExamPaper(ep);
            tableRowVM.setSubject(subjectService.getById(ep.getSubjectId()));
            tableData.add(tableRowVM);
        });
        return Result.success(tableData);
    }

    @GetMapping
    public Result getExamPaperList() {
        List<TableRowVM> tableData = new ArrayList<>();
        List<ExamPaper> examPapers = examPaperService.getAll();
        examPapers.forEach(ep -> {
            TableRowVM tableRowVM = new TableRowVM();
            tableRowVM.setExamPaper(ep);
            tableRowVM.setSubject(subjectService.getById(ep.getSubjectId()));
            tableData.add(tableRowVM);
        });
        return Result.success(tableData);
    }

    @DeleteMapping("/{id}")
    public Result removeById(@PathVariable Integer id) {
        examPaperService.deleteById(id);
        return Result.success();
    }

}
